<template>
  <div>
    <el-card>
      <div slot="header"
           class="clearfix">
        <span>订单详情</span>
      </div>
      <el-form ref="orderForm"
               :model="orderForm"
               :rules="formRule"
               label-position="right"
               label-width="120px"
               style="width:100%;flex-wrap:wrap"
               class="flex">
        <el-form-item label="订单号"
                      prop="orderNum"
                      style="width:30%">
          <el-input v-model="orderForm.orderNum"
                    placeholder=""
                    :disabled="isDisabled"></el-input>
        </el-form-item>
        <el-form-item label="订单时间"
                      prop="addTime"
                      style="width:30%">
          <el-input v-model="orderForm.addTime"
                    placeholder=""
                    :disabled="isDisabled"></el-input>
        </el-form-item>
         <el-form-item label="订单状态"
                      prop="status"
                      style="width:30%;">
          <el-input v-model="orderForm.status"
                    placeholder=""
                    :disabled="isDisabled"></el-input>
        </el-form-item>
        <el-form-item label="付款方式"
                      prop="payType"
                      style="width:30%;">
          <el-input v-model="orderForm.payType"
                    placeholder=""
                    :disabled="isDisabled"></el-input>
        </el-form-item>
         <el-form-item label="订单金额"
                      prop="totalPrice"
                      style="width:30%;">
          <el-input v-model="orderForm.totalPrice"
                    placeholder=""
                    :disabled="isDisabled"></el-input>
        </el-form-item>
        <el-form-item label="订单利润"
                      prop="profitPrice"
                      style="width:30%;">
          <el-input v-model="orderForm.profitPrice"
                    placeholder=""
                    :disabled="isDisabled"></el-input>
        </el-form-item>
        <el-form-item label="物流成本"
                      prop="totalPostage"
                      style="width:30%;">
          <el-input v-model="orderForm.totalPostage"
                    placeholder=""
                    :disabled="isDisabled"></el-input>
        </el-form-item>
        <el-form-item label="购买账号"
                      prop="realName"
                      style="width:30%;">
          <el-input v-model="orderForm.realName"
                    placeholder=""
                    :disabled="isDisabled"></el-input>
        </el-form-item>
        <el-form-item label="购买用户手机"
                      prop="userPhone"
                      style="width:30%;">
          <el-input v-model="orderForm.userPhone"
                    placeholder=""
                    :disabled="isDisabled"></el-input>
        </el-form-item>
        <el-form-item label="收货人姓名"
                      prop="realName"
                      style="width:30%;">
          <el-input v-model="orderForm.realName"
                    placeholder=""
                    :disabled="isDisabled"></el-input>
        </el-form-item>
        <el-form-item label="收货人电话"
                      prop="userPhone"
                      style="width:30%;">
          <el-input v-model="orderForm.userPhone"
                    placeholder=""
                    :disabled="isDisabled"></el-input>
        </el-form-item>
        <el-form-item label="收货人地址"
                      prop="userAddress"
                      style="width:30%;">
          <el-input v-model="orderForm.userAddress"
                    placeholder=""
                    :disabled="isDisabled"></el-input>
        </el-form-item>
      </el-form>
      <el-table :data="productListData"
                style="width: 100%"
                height="400"
                stripe>

        <el-table-column prop="productCode"
                         label="制造商料号"
                         align="center"></el-table-column>
        <el-table-column prop="productCode"
                         label="AKS料号"
                         align="center"></el-table-column>
        <el-table-column prop="menuName"
                         label="供应商"
                         align="center"></el-table-column>
        <el-table-column prop="status"
                         label="商品状态"
                         align="center"></el-table-column>
        <el-table-column prop="buyNum"
                         label="购买数量"
                         align="center"></el-table-column>
        <el-table-column prop="price"
                         label="单价"
                         align="center"></el-table-column>
        <el-table-column prop="totalMoney"
                         label="总金额"
                         align="center"></el-table-column>
        <el-table-column prop="costPrice"
                         label="成本总价"
                         align="center"></el-table-column>
        <el-table-column prop="profit"
                         label="商品利润"
                         align="center"></el-table-column>
      </el-table>  
      <el-pagination style="text-align:center;margin-top:20px"
                     @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :current-page="currentPage"
                     :page-sizes="[5, 10, 15, 20]"
                     :page-size="pageSize"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="total"></el-pagination>
      <!--<div class="flex" style="justify-content:center;margin:15px 0" v-show="orderForm.paid==0" >
        <el-button type="primary" size="medium" v-if="isDisabled"  @click="dialogVisible = true">确认订单</el-button>
        <el-button type="danger" size="medium" style="margin-left:50px"  @click="redialogVisible = true" v-if="isDisabled">打回订单</el-button>
      </div>-->
    </el-card>
  </div>
</template>

<script>
import {
  ORDER_CART_LIST,ORDER_PRODUCT_LIST,
  ORDER_AGREE, ORDER_REFUSE, ADMINCONFIRM
} from '@/utils/api';
import { formatDate } from '@/utils/validate.js';
import axios from 'axios'
export default {
  data () {
    return {
      mainId:null,
      rowData:{},
      sendVisible:false,
      dialogVisible: false,
      redialogVisible: false,
      orderForm: {},
      formRule: {},
      productListData: [
      ],
      payOptions: [
        {
          value: "0",
          label: "未支付"
        },
        {
          value: "1",
          label: "已支付"
        },
        {
          value: "3",
          label: "线下处理"
        }
      ],
      payMethods: [
        {
          value: "0",
          label: "银行卡"
        },
        {
          value: "1",
          label: "线下支付"
        }
      ],
      orderOptions: [
        {
          value: "0",
          label: "未打回"
        },
        {
          value: "1",
          label: "已打回"
        },
        {
          value: "2",
          label: "未确认"
        }
      ],
      isDisabled: false,
      currentPage: 1,
      pageSize: 10,
      total: 0,
    };
  },
  filters: {
    statusFilter (val) {
      console.log(val)
      // if (val == "待付款") {
      //   val.status = 0
      // } else if (val.status == "待收货") {
      //   val.status = 1
      // } else if (val.status == "待发货") {
      //   val.status = 2
      // } else if (val.status == "待评价") {
      //   val.status = 3
      // } else if (val.status == "已评价") {
      //   val.status = 6
      // } else if (val.status == "退换货订单") {
      //   val.status = -1
      // } else if (val.status == "已完成") {
      //   val.status = 4
      // }
      return val == "待付款"
        ? true
        : val == "待收货"
          ? true
          : val == "待发货"
            ? false
            : val == "待评价"
              ? false
              : val == "已完成"
                ? false
                : val == "已评价"
                  ? false
                  : val == "退换货订单" ? false : false;
    }
  },
  mounted () {
    if (this.$route.query["data"]) {
      this.isDisabled = this.$route.query["handler"] == "check" ? true : false;
      this.orderForm = this.$route.query["data"];
      console.log(this.orderForm)
      this.mainId=this.orderForm.id
      if (this.orderForm.paid == 1) {
        this.orderForm.paid = '已支付'
      } else if (this.orderForm.paid == 2) {
        this.orderForm.paid = '线下审核'
      } else {
        this.orderForm.paid = '未支付'
      }

      if (this.orderForm.status == 0) {
        this.orderForm.status = "待付款"
      } else if (this.orderForm.status == 1) {
        this.orderForm.status = "待收货"
      } else if (this.orderForm.status == 2) {
        this.orderForm.status = "待发货"
      } else if (this.orderForm.status == 3) {
        this.orderForm.status = "待评价"
      } else if (this.orderForm.status == 6) {
        this.orderForm.status = "已评价"
      } else if (this.orderForm.status == -1) {
        this.orderForm.status = "退换货订单"
      } else if (this.orderForm.status == 4) {
        this.orderForm.status = "已完成"
      }
      this.getOrderList()
    }
  },
  methods: {
    // 确认发货
    sendPro (val) {
      sendVisible=true
      this.rowData=val
      console.log(val)
    },
    sendProduct(){
      var val=this.rowData
      console.log(val)
      let th = this
      if (val.status == "待付款") {
        val.status = 0
      } else if (val.status == "待收货") {
        val.status = 1
      } else if (val.status == "待发货") {
        val.status = 2
      } else if (val.status == "待评价") {
        val.status = 3
      } else if (val.status == "已评价") {
        val.status = 6
      } else if (val.status == "退换货订单") {
        val.status = -1
      } else if (val.status == "已完成") {
        val.status = 4
      }
      console.log(val.status, val.unique)
      let param = {
        uni: val.unique,
        status: 1
      }  // 创建form对象
      let config = {
        headers: { 'Content-Type': 'application/json' }
      }
      axios.post('http://fox.foxcore.cn/api/v1/redirect/adminConfirm', param, config).then(res => {
        this.getOrderList()
      })
    },
    rehandleClose (done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => { });
    },
    handleClose (done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(res => { });
    },
    refuseOrder () {
      ORDER_REFUSE({
        orderNum: this.orderForm.orderId,
      }).then(res => {
        this.$message.success('打回操作成功')
        this.getOrderList()
        this.redialogVisible = false
      })
    },
    agreeOrder () {
      ORDER_AGREE({
        orderNum: this.orderForm.orderId,
      }).then(res => {
        this.$message.success('同意操作成功')
        this.getOrderList()
        this.dialogVisible = false
      })
    },
    getOrderList () {
      console.log('===>'+this.mainId)
      ORDER_PRODUCT_LIST({ 
          mainId: this.mainId,
          pageNo:this.currentPage,
          pageSize:this.pageSize
      }).then(res => {
          this.productListData = res.data.dataList
          this.total = res.data.count
          console.log('==res=>'+JSON.stringify(this.productListData))
          for (let i in this.productListData) {
            this.productListData[i].addTime = this.formatSecond(this.productListData[i].addTime)
            //处理状态
            switch(this.productListData[i].sonRefundStatus){
              case 2:
              case 4:
              case 5:
                this.productListData[i].sonRefundStatus2 = "已处理"
                break
              default:
                this.productListData[i].sonRefundStatus2 = "未处理"
                break;
            }
            //申请类型
            switch(this.productListData[i].sonRefundStatus){
              case 2:
              case -2:
                this.productListData[i].sonRefundStatus = "退款"
                break
              case 4:
              case -4:
                this.productListData[i].sonRefundStatus = "换货"
                break;
              case 5:
              case -5:
                this.productListData[i].sonRefundStatus = "退款换货"
                break;
              case -10:
                this.productListData[i].sonRefundStatus = "拒绝"
                break;
              case -20:
                this.productListData[i].sonRefundStatus = "申请撤销"
                break;
            }
            
          }
          console.log('==res2=>'+JSON.stringify(this.productListData))
      })
    },
    //时间格式化  
    formatSecond (second) {
      if (!second) return "";
      return formatDate(new Date(second * 1000), "yyyy-MM-dd hh:mm:ss");
    },
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val
      this.getOrderList()
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val
      this.getOrderList()
    }
  }
};
</script>

<style lang="scss" scoped>
</style>